package com.four.bergain.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.four.bergain.dao.BargainPropertyMapper;
import com.four.bergain.entity.Bargain;
import com.four.bergain.dao.BargainMapper;
import com.four.bergain.entity.BargainProperty;
import com.four.bergain.feign.ShopFeginService;
import com.four.bergain.service.IBargainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.four.bergain.vo.BargainPropertyVo;
import com.four.bergain.vo.BargainVo;
import com.guli.common.utils.R;
import com.guli.common.vo.ManageVo;
import com.guli.common.vo.ParameterVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gs
 * @since 2021-12-19
 */
@Service
public class BargainServiceImpl extends ServiceImpl<BargainMapper, Bargain> implements IBargainService {

    @Autowired
    private BargainMapper bargainDao;
    @Autowired
    private BargainPropertyMapper propertyMapper;
    @Autowired
    private ShopFeginService shopFeginService;


    @Override
    public IPage<Bargain> getList(Integer page, Integer pageSize, BargainVo vo) {
        Page<Bargain> goodsVoPage = new Page<>(page, pageSize);
        return bargainDao.getPage(goodsVoPage,vo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R saveBargain(BargainPropertyVo vo) {
        //添加砍价商品
        Bargain bargain = new Bargain();
        BeanUtils.copyProperties(vo,bargain);
        bargainDao.saveBargain(bargain);

        //添加砍价规格
        BargainProperty property = new BargainProperty();
        BeanUtils.copyProperties(vo,property);
        property.setBaId(bargain.getBaId());
        propertyMapper.saveBargainProperty(property);
        return R.ok();
    }

    @Override
    public R updateBargain(BargainPropertyVo vo) {
        //添加砍价商品
        Bargain bargain = new Bargain();
        BeanUtils.copyProperties(vo,bargain);
        bargainDao.updateBargain(bargain);

        //添加砍价规格
        BargainProperty property = new BargainProperty();
        BeanUtils.copyProperties(vo,property);
        propertyMapper.updateBargainProperty(property);
        return R.ok();
    }

    @Override
    public R getPictureById(Integer manId) {

        return shopFeginService.getPictureById(manId);
    }

    @Override
    public R findPropertyById(Integer manId) {
        List<ParameterVo> parameterById = shopFeginService.findParameterById(manId);
        return R.ok().put("property",parameterById);
    }

    @Override
    public R updateStatusOne(Integer baId) {
        bargainDao.updateStatusOne(baId);
        return R.ok();
    }

    @Override
    public R updateStatusTwo(Integer baId) {
        bargainDao.updateStatusTwo(baId);
        return R.ok();
    }

    @Override
    public R shopList() {
        List<ManageVo> allManage = shopFeginService.findAllManage();
        return R.ok().put("data",allManage);
    }

    @Override
    public R getShopById(Integer manId) {

        ManageVo manageVo=shopFeginService.getManageById(manId);
        return R.ok().put("manageVo",manageVo);
    }
}
